package com.graduate.service.impl;

import java.util.List;
import java.util.Map;


//import org.apache.cxf.common.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.graduate.mapper.SysRoleMapper;
import com.graduate.mapper.SysUserMapper;
import com.graduate.model.SysRole;
import com.graduate.model.SysUser;
import com.graduate.service.ISysUserService;
import com.graduate.util.PageInfo;

@Service("userService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService{
	@Autowired
	private SysUserMapper userMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	//private RoleMapper roleMapper;
	public PageInfo getPageInfo(PageInfo pageInfo) {
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getPagesize());
		List<Map<String, Object>> list = userMapper.getPageInfoNoConcat(page, pageInfo.getCondition());
		pageInfo.setRows(list);
		pageInfo.setTotal(page.getTotal());
		return pageInfo;
	}
	@Override
	@Transactional(isolation=Isolation.DEFAULT)
	public Integer saveUser(SysUser user) {
		Integer result = userMapper.insert(user);// 插入新增用户信息
		result += userMapper.saveBatchUserAndRole(user);// 批量插入用户与角色关联表信息
		
		return result;
	}
	@Override
	@Transactional(isolation=Isolation.DEFAULT)
	public Integer updateUser(SysUser user) {
		Integer result = userMapper.updateById(user);// 根据用户ID更新用户信息
		result += userMapper.deleteBatchUserAndRoleByUserId(user.getId());// 根据用户ID批量删除用户与角色中间表信息
		result += userMapper.saveBatchUserAndRole(user);// 批量插入用户与角色关联表信息
		
		return result;
	}
	@Override
	@Transactional(isolation=Isolation.DEFAULT)
	public Integer deleteUser(SysUser user) {
		Integer result = userMapper.updateBatchUserToDel(user);// 批量将用户信息更新为作废
		result += userMapper.deleteBatchUserAndRoleByUserList(user);// 根据用户列表批量删除用户与角色中间表信息
		return result;
	}

	public SysUser selectOne(SysUser user) {
		try {
			return userMapper.selectUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Map<String, Object> getUserInfoById(Map<String, Object> map) {
		Map<String, Object> mapTem = userMapper.getUserInfoById(map);
		if(!("".equals(mapTem.get("id").toString()))){
			  //循环并将用户权限返回
			List<SysRole> rolelist =sysRoleMapper.getRoleListByUserId(mapTem.get("id").toString());
			if(rolelist.size()>0){
				String role_names = "";
				String role_ids = "";
				for(SysRole role:rolelist){
					if(role_names.equals("")){
						role_names += role.getName();
						role_ids += role.getId();
					}else{
						role_names += ","+role.getName();
						role_ids += ","+role.getId();
					}
				} 
				mapTem.put("role_names", role_names);
				mapTem.put("role_ids", role_ids);
			}
		}
		return mapTem;
	}

	public Integer updateBatchUserWithOrg(SysUser user) {

		return userMapper.updateBatchUserWithOrg(user);
	}

	public SysUser selectUser(SysUser user) {
		return userMapper.selectUser(user);
	}

	public List<SysUser> getUserByOrgCode(String orgCode, String roleCode) {
		
		return null;
	}

	@Override
	public PageInfo getPageInfoForSelector(PageInfo pageInfo) {
		
		//userMapper.getPageInfoForSelector(page, condition);
		return null;
	}



}
